package test;

import com.edu.bean.Book;
import com.edu.bean.Grade;
import com.edu.bean.Student;
import com.edu.bean.Type;
import com.edu.mapper.BookMapper;
import com.edu.mapper.GradeMapper;
import com.edu.mapper.TypeMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MyTest04 {

    SqlSession sqlSession = null;

    @Before
    public void init() throws IOException {
        String path = "mybatis-config.xml";//路径
        InputStream inputStream = Resources.getResourceAsStream(path);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sessionFactory.openSession();
    }

    /*
     *   多对一: 利用map接受查询结果
     * */
    @Test
    public void test01(){
        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
        Book book = bookMapper.selectBookAndTypeById(4);
        System.out.println(book);
        System.out.println(book.getTypeList());

    }

    @Test
    public void test04(){
        TypeMapper typeMapper = sqlSession.getMapper(TypeMapper.class);
        Type type = typeMapper.selectTypeAndBooksByTypeId(1);
        System.out.println(type);
        System.out.println(type.getBookList());

    }


  

}
